public class MaxSubarraySum {
    public static void main(String[] args) {
        int[] arr1 = {1, -2, 3, 5, -1};
        System.out.println("数组1的最大子数组和：" + findMaxSubarraySum(arr1));      
        int[] arr2 = {1, -2, 3, -8, 5, 1};
        System.out.println("数组2的最大子数组和：" + findMaxSubarraySum(arr2));
        int[] arr3 = {1, -2, 3, -2, 5, 1};
        System.out.println("数组3的最大子数组和：" + findMaxSubarraySum(arr3));
    }
    
public static int findMaxSubarraySum(int[] arr) {
        if (arr == null || arr.length == 0) {
            throw new IllegalArgumentException("数组不能为空");
        }
        
        int maxCurrent = arr[0];  
        int maxGlobal = arr[0];   
        
        for (int i = 1; i < arr.length; i++) {
            maxCurrent = Math.max(arr[i], maxCurrent + arr[i]);
            if (maxCurrent > maxGlobal) {
                maxGlobal = maxCurrent;
            }
        }        
        return maxGlobal;
    }
}
